package dal

import (
	"context"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"gorm.io/gen"
	"gorm.io/gen/field"
	"mall_admin_api/biz/model"
)

type AdministratorDAO struct {
	bizID int64
}

type AdministratorListParams struct {
	Condition []gen.Condition
	Page      int
	Size      int
	Sort      []field.Expr
}

func newAdministratorDAO(bizID int64) *AdministratorDAO {
	return &AdministratorDAO{
		bizID: bizID,
	}
}

func (c *AdministratorDAO) MGetAdministrator(ctx context.Context, ids []int64) (map[int64]*model.Administrator, error) {
	list, err := Administrator.WithContext(ctx).Where(

		Administrator.ID.In(ids...),
		Administrator.DeletedAt.IsNull(),
	).Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.MGetAdministrator err: %v", err)
		return nil, err
	}
	var data = make(map[int64]*model.Administrator)
	for _, info := range list {
		data[info.ID] = info
	}
	return data, nil
}

func (c *AdministratorDAO) GetAdministratorByConditions(ctx context.Context, condition []gen.Condition) (*model.Administrator, error) {
	tx := Administrator.WithContext(ctx).Where(

		Administrator.DeletedAt.IsNull(),
	)
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	info, err := tx.First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetAdministratorByConditions failed, %s", err.Error())
		return nil, err
	}
	return info, nil
}

func (c *AdministratorDAO) GetAdministratorList(ctx context.Context, condition []gen.Condition, page, size int) ([]*model.Administrator, error) {
	tx := Administrator.WithContext(ctx).Where(Administrator.DeletedAt.IsNull())
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	if page > 0 && size > 0 {
		tx = tx.Offset((page - 1) * size).Limit(size)
	}
	list, err := tx.Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetAdministratorList err: %v", err)
		return nil, err
	}
	return list, nil
}

func (c *AdministratorDAO) GetAdministratorTotal(ctx context.Context, condition []gen.Condition) (int64, error) {
	total, err := Administrator.WithContext(ctx).Where(Administrator.DeletedAt.IsNull()).Where(condition...).Count()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetAdministratorTotal error: %v", err)
		return 0, err
	}
	return total, nil
}

func (c *AdministratorDAO) GetAdministratorInfoByID(ctx context.Context, id int64) (*model.Administrator, error) {
	info, err := Administrator.WithContext(ctx).Where(Administrator.DeletedAt.IsNull()).Where(Administrator.ID.Eq(id)).First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetAdministratorInfoByID error: %v", err)
		return nil, err
	}
	return info, nil
}

func (c *AdministratorDAO) GetListWithTotal(ctx context.Context, params *AdministratorListParams) ([]*model.Administrator, int64, error) {
	tx := Administrator.WithContext(ctx).Where(Administrator.DeletedAt.IsNull())
	if len(params.Condition) > 0 {
		tx = tx.Where(params.Condition...)
	}
	if len(params.Sort) > 0 {
		tx = tx.Order(params.Sort...)
	}
	list, total, err := tx.FindByPage((params.Page-1)*params.Size, params.Size)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.Administrator.GetListWithTotal err: %v", err)
		return nil, 0, err
	}
	return list, total, nil
}

func (c *AdministratorDAO) CreateAdministrator(ctx context.Context, data *model.Administrator) (*model.Administrator, error) {
	if err := Administrator.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateAdministrator error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *AdministratorDAO) CreateAdministratorWithTx(ctx context.Context, tx *Query, data *model.Administrator) (*model.Administrator, error) {
	if err := tx.Administrator.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateAdministrator error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *AdministratorDAO) BatchCreateAdministrator(ctx context.Context, data []*model.Administrator) error {
	if err := Administrator.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateInBatchesAdministrator error: %v", err)
		return err
	}
	return nil
}

func (c *AdministratorDAO) BatchCreateAdministratorWithTx(ctx context.Context, tx *Query, data []*model.Administrator) ([]*model.Administrator, error) {
	if err := tx.Administrator.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.BatchCreateWithTxAdministrator error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *AdministratorDAO) UpdateAdministrator(ctx context.Context, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := Administrator.WithContext(ctx).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UpdateAdministrator error: %v", err)
		return err
	}
	return nil
}

func (c *AdministratorDAO) UpdateAdministratorWithTx(ctx context.Context, tx *Query, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := tx.Administrator.WithContext(ctx).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UpdateAdministrator error: %v", err)
		return err
	}
	return nil
}

func (c *AdministratorDAO) DeleteAdministrator(ctx context.Context, condition []gen.Condition) error {
	_, err := Administrator.WithContext(ctx).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.DeleteAdministrator error: %v", err)
		return err
	}
	return nil
}

func (c *AdministratorDAO) DeleteAdministratorWithTx(ctx context.Context, tx *Query, condition []gen.Condition) error {
	_, err := tx.Administrator.WithContext(ctx).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.DeleteAdministratorWithTx error: %v", err)
		return err
	}
	return nil
}
